package de.urlaub.server.logic;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.interceptor.Interceptors;

import de.urlaub.client.data.Division;
import de.urlaub.client.data.Employee;
import de.urlaub.client.data.Vacation;
import de.urlaub.client.data.VacationDao;
import de.urlaub.server.logging.LoggingInterceptor;

@Stateless
@Interceptors(LoggingInterceptor.class)
public class DataUtil implements DataUtilRemote
{	
	@Inject
	private VacationDao vacationDao;
	
	private ArrayList<Vacation> vacations;
	private ArrayList<Employee> employees;
		
	@Override
	public void createSampleData()
	{
		Division div = new Division(2, "IT", false);
		vacationDao.persistDivision(div);
		Employee employee = new Employee("td", "Tobias", "Dehn", "pw", "uhpjee@gmail.com", div);
		vacationDao.persistEmployee(employee);
		
		Vacation vacation = new Vacation(employee, new Date(2012, 10, 1), new Date(2012, 10, 2), 0);
		vacationDao.persistVacation(vacation);
		
		div = new Division(1, "Management", true);
		vacationDao.persistDivision(div);
		employee = new Employee("jr", "Jörg", "Rettschlag", "pw", "uhpjee@gmail.com", div);
		vacationDao.persistEmployee(employee);

		vacation = new Vacation(employee, new Date(2012, 10, 8), new Date(2012, 10, 14), 0);
		vacationDao.persistVacation(vacation);
	}
	
	@Override
	public String[] getVacationColumnNames(boolean showExtendedView)
	{
		String[] columnNames;
		
		if (showExtendedView)
			columnNames = new String[4];
		else
			columnNames = new String[3];
		
		int i = 0;
		columnNames[i] = "ID";
		i++;
		if (showExtendedView) {
			columnNames[i] = "Mitarbeiter";
			i++;
		}
		columnNames[i] = "Von";
		i++;
		columnNames[i] = "Bis";
		
		return columnNames;
	}
	
	@Override
	public Object[][] getVacationTableData(boolean showExtendedView)
	{
		Object[][] data;
		if (this.vacations == null)
			data = new Object[0][0];
		else {
			if (showExtendedView)
				data = new Object[this.vacations.size()][4];
			else
				data = new Object[this.vacations.size()][3];
			
			for (int i = 0; i < this.vacations.size(); i++)
			{
				Vacation currentVacation = this.vacations.get(i);
				int j = 0;
				data[i][j] = currentVacation.getVacationId();
				j++;
				if (showExtendedView) {
					data[i][j] = currentVacation.getEmployee().getFirstName() 
								 + currentVacation.getEmployee().getLastName();
					j++;
				}
				data[i][j] = currentVacation.getFromDate();
				j++;
				data[i][j] = currentVacation.getTillDate();
			}
		}
		
		return data;
	}
	
	@Override 
	public void queryVacations(int state)
	{
		queryVacations(state, null);
	}
	
	@Override
	public void queryVacations(int state, Employee employee)
	{
		if (employee == null)
			this.vacations = this.vacationDao.getVacations(state);
		else
			this.vacations = this.vacationDao.getVacations(employee, state);
	}
	
	@Override
	public ArrayList<Vacation> getVacations()
	{
		return this.vacations;
	}
	
	@Override
	public Vacation getVacation(int vacationId)
	{
		return this.vacationDao.getVacation(vacationId);
	}
	
	@Override
	public void updateVacations(HashMap<Integer, Boolean> changedVacations)
	{
		this.vacationDao.updateVacations(changedVacations);
	}
	
	@Override 
	public ArrayList<Employee> getEmployees()
	{
		if (this.employees == null)
			queryEmployees();
		
		return this.employees;
	}
	
	private void queryEmployees()
	{
		this.employees = this.vacationDao.getEmployees();
	}
}
